# -*- coding: utf-8 -*-
# @Time    : 2024/7/8 10:22
# @Author  : wangwei
# @File    : autopro.py
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from common.verify import get_verity
from get_path import get_pathdir

#去掉自动化控制：
option = Options()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("--disable-blink-features=AutomationControlled")
#实现不关闭的重点
option.add_experimental_option("detach", True)
#关闭密码弹框：
option.add_experimental_option("prefs", {
    "download.prompt_for_download": False,
    "download.directory_upgrade": True,
    "safebrowsing.enabled": True,
    "credentials_enable_service": False,
    "profile.password_manager_enabled": False
})
option.add_experimental_option("excludeSwitches", ['enable-automation'])
driver=webdriver.Chrome(options=option)
driver.implicitly_wait(10)
driver.maximize_window()
#1.账号登录
driver.get("http://testingedu.com.cn:8000/index.php/Home/user/login.html")
driver.find_element(By.XPATH,'//*[@id="username"]').send_keys("13800138006")
driver.find_element(By.XPATH,'//*[@id="password"]').send_keys("123456")
driver.find_element(By.XPATH,'//*[@id="verify_code_img"]').screenshot(f'{get_pathdir}\\data\\.png')
driver.find_element(By.XPATH,'//*[@id="verify_code"]').send_keys(f"{get_verity()}")
driver.find_element(By.XPATH,'//*[@class="J-login-submit"]').click()

#2.修改个人信息
driver.find_element(By.XPATH,'//li/a[text()="个人信息"]').click()
driver.find_element(By.XPATH,'//*[@id="preview"]').click()
driver.switch_to.frame("layui-layer-iframe1")
driver.find_element(By.XPATH,'//*[@id="filePicker"]/div/input').send_keys(f'{get_pathdir}/data/ai.jpg')
driver.find_element(By.XPATH,'//div[text()="确定使用"]').click()
driver.switch_to.default_content()
time.sleep(3)
driver.find_element(By.XPATH,'//*[@value="确认保存"]').click()


#3.新增收货地址
driver.get("http://testingedu.com.cn:8000/index.php/Home/User/address_list.html")
driver.find_element(By.XPATH,'//span[text()="增加新地址"]').click()
time.sleep(3)
driver.find_element(By.XPATH,'//input[@name="consignee"]').send_keys("西门吹雪")
driver.find_element(By.XPATH,'//input[@name="mobile"]').send_keys("18312345678")
time.sleep(3)
ele=driver.find_element(By.XPATH,'//*[@id="province"]')
select=Select(ele)

select.select_by_visible_text("湖南省")
time.sleep(1)
ele=driver.find_element(By.XPATH,'//*[@id="city"]')
select=Select(ele)
select.select_by_visible_text("长沙市")
time.sleep(1)
ele=driver.find_element(By.XPATH,'//*[@id="district"]')
select=Select(ele)
select.select_by_visible_text("岳麓区")
time.sleep(1)
ele=driver.find_element(By.XPATH,'//*[@id="twon"]')
select=Select(ele)
select.select_by_visible_text("岳麓街道")
time.sleep(1)
driver.find_element(By.XPATH,'//input[@name="address"]').send_keys("这是详细地址")
time.sleep(1)
driver.find_element(By.XPATH,'//a[@id="address_submit"]').click()
time.sleep(1)

#4.删除收货地址
driver.get("http://testingedu.com.cn:8000/index.php/Home/User/address_list.html")
driver.find_element(By.XPATH,'//a[text()="删除"]').click()